🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / app / src / main / java / com / geeksville / mesh / repository / bluetooth / BluetoothRepository.kt
Displaying Raw • Download
app/src/main/java/com/geeksville/mesh/repository/bluetooth/BluetoothRepository.kt ui_message_list (be3f882f) Text, 5.42 KB
T8b949e/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787com.geeksville.mesh.repository.bluetooth
Tff7b72import T7ee787android.app.Application
Tff7b72import T7ee787android.bluetooth.BluetoothAdapter
Tff7b72import T7ee787android.bluetooth.BluetoothDevice
Tff7b72import T7ee787android.bluetooth.le.BluetoothLeScanner
Tff7b72import T7ee787android.bluetooth.le.ScanFilter
Tff7b72import T7ee787android.bluetooth.le.ScanResult
Tff7b72import T7ee787android.bluetooth.le.ScanSettings
Tff7b72import T7ee787androidx.annotation.RequiresPermission
Tff7b72import T7ee787androidx.lifecycle.Lifecycle
Tff7b72import T7ee787androidx.lifecycle.coroutineScope
Tff7b72import T7ee787com.geeksville.mesh.CoroutineDispatchers
Tff7b72import T7ee787com.geeksville.mesh.android.Logging
Tff7b72import T7ee787com.geeksville.mesh.android.hasBluetoothPermission
Tff7b72import T7ee787com.geeksville.mesh.util.registerReceiverCompat
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.MutableStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.StateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.asStateFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.emptyFlow
Tff7b72import T7ee787kotlinx.coroutines.flow.filter
Tff7b72import T7ee787kotlinx.coroutines.launch
Tff7b72import T7ee787javax.inject.Inject
Tff7b72import T7ee787javax.inject.Singleton
T8b949e/** Repository responsible for maintaining and updating the state of Bluetooth availability. */
Tf0883e@Singleton
Tff7b72class T56d364BluetoothRepository
Tf0883e@Inject
Tff7b72constructorTb4b4b4(
Tff7b72private Tff7b72val Te6edf3applicationTb4b4b4: Te6edf3ApplicationTb4b4b4,
Tff7b72private Tff7b72val Te6edf3bluetoothAdapterLazyTb4b4b4: Te6edf3daggerTb4b4b4.Te6edf3LazyTff7b72<Te6edf3BluetoothAdapter?Tff7b72>Tb4b4b4,
Tff7b72private Tff7b72val Te6edf3bluetoothBroadcastReceiverLazyTb4b4b4: Te6edf3daggerTb4b4b4.Te6edf3LazyTff7b72<Te6edf3BluetoothBroadcastReceiverTff7b72>Tb4b4b4,
Tff7b72private Tff7b72val Te6edf3dispatchersTb4b4b4: Te6edf3CoroutineDispatchersTb4b4b4,
Tff7b72private Tff7b72val Te6edf3processLifecycleTb4b4b4: Te6edf3LifecycleTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3Logging Tb4b4b4{
Tff7b72private Tff7b72val Te6edf3_state Tff7b72=
Te6edf3MutableStateFlowTb4b4b4(
Te6edf3BluetoothStateTb4b4b4(
T8b949e// Assume we have permission until we get our initial state update to prevent premature
T8b949e// notifications to the user.
Te6edf3hasPermissions Tff7b72= Tff7b72trueTb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tff7b72val Te6edf3stateTb4b4b4: Te6edf3StateFlowTff7b72<Te6edf3BluetoothStateTff7b72> Tff7b72= Te6edf3_stateTb4b4b4.Te6edf3asStateFlowTb4b4b4(Tb4b4b4)
Tff7b72init Tb4b4b4{
Te6edf3processLifecycleTb4b4b4.Te6edf3coroutineScopeTb4b4b4.Te6edf3launchTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3defaultTb4b4b4) Tb4b4b4{
Te6edf3updateBluetoothStateTb4b4b4(Tb4b4b4)
Te6edf3bluetoothBroadcastReceiverLazyTb4b4b4.Te6edf3getTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3let Tb4b4b4{ Te6edf3receiver Tff7b72-Tff7b72>
Te6edf3applicationTb4b4b4.Te6edf3registerReceiverCompatTb4b4b4(Te6edf3receiverTb4b4b4, Te6edf3receiverTb4b4b4.Te6edf3intentFilterTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tff7b72fun Td2a8ffrefreshStateTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3processLifecycleTb4b4b4.Te6edf3coroutineScopeTb4b4b4.Te6edf3launchTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3defaultTb4b4b4) Tb4b4b4{ Te6edf3updateBluetoothStateTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4}
T8b949e/** @return true for a valid Bluetooth address, false otherwise */
Tff7b72fun Td2a8ffisValidTb4b4b4(Te6edf3bleAddressTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Tffa657Boolean Tff7b72= Te6edf3BluetoothAdapterTb4b4b4.Te6edf3checkBluetoothAddressTb4b4b4(Te6edf3bleAddressTb4b4b4)
Tff7b72fun Td2a8ffgetRemoteDeviceTb4b4b4(Te6edf3addressTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3BluetoothDevice? Tff7b72= Te6edf3bluetoothAdapterLazy
Tb4b4b4.Te6edf3getTb4b4b4(Tb4b4b4)
Tff7b72?.Te6edf3takeIf Tb4b4b4{ Te6edf3applicationTb4b4b4.Te6edf3hasBluetoothPermissionTb4b4b4(Tb4b4b4) Tff7b72&Tff7b72& Te6edf3isValidTb4b4b4(Te6edf3addressTb4b4b4) Tb4b4b4}
Tff7b72?.Te6edf3getRemoteDeviceTb4b4b4(Te6edf3addressTb4b4b4)
Tff7b72private Tff7b72fun Td2a8ffgetBluetoothLeScannerTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3BluetoothLeScanner? Tff7b72=
Te6edf3bluetoothAdapterLazyTb4b4b4.Te6edf3getTb4b4b4(Tb4b4b4)Tff7b72?.Te6edf3takeIf Tb4b4b4{ Te6edf3applicationTb4b4b4.Te6edf3hasBluetoothPermissionTb4b4b4(Tb4b4b4) Tb4b4b4}Tff7b72?.Te6edf3bluetoothLeScanner
Tff7b72fun Td2a8ffscanTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3FlowTff7b72<Te6edf3ScanResultTff7b72> Tb4b4b4{
Tff7b72val Te6edf3filter Tff7b72=
Te6edf3ScanFilterTb4b4b4.Te6edf3BuilderTb4b4b4(Tb4b4b4)
T8b949e// Samsung doesn't seem to filter properly by service so this can't work
T8b949e// see
T8b949e// https://stackoverflow.com/questions/57981986/altbeacon-android-beacon-library-not-working-after-device-has-screen-off-for-a-s/57995960#57995960
T8b949e// and https://stackoverflow.com/a/45590493
T8b949e// .setServiceUuid(ParcelUuid(BluetoothInterface.BTM_SERVICE_UUID))
Tb4b4b4.Te6edf3buildTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3settings Tff7b72= Te6edf3ScanSettingsTb4b4b4.Te6edf3BuilderTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3setScanModeTb4b4b4(Te6edf3ScanSettingsTb4b4b4.Te6edf3SCAN_MODE_LOW_LATENCYTb4b4b4)Tb4b4b4.Te6edf3buildTb4b4b4(Tb4b4b4)
Tff7b72return Te6edf3getBluetoothLeScannerTb4b4b4(Tb4b4b4)Tff7b72?.Te6edf3scanTb4b4b4(Te6edf3listOfTb4b4b4(Te6edf3filterTb4b4b4)Tb4b4b4, Te6edf3settingsTb4b4b4)Tff7b72?.Te6edf3filter Tb4b4b4{
Tffa657itTb4b4b4.Te6edf3deviceTb4b4b4.Te6edf3nameTff7b72?.Te6edf3matchesTb4b4b4(Te6edf3RegexTb4b4b4(Te6edf3BLE_NAME_PATTERNTb4b4b4)Tb4b4b4) Tff7b72=Tff7b72= Tff7b72true
Tb4b4b4} Tff7b72?: Te6edf3emptyFlowTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tf0883e@RequiresPermissionTb4b4b4(Ta5d6ff"Ta5d6ffandroid.permission.BLUETOOTH_CONNECTTa5d6ff"Tb4b4b4)
Tff7b72fun Td2a8ffcreateBondTb4b4b4(Te6edf3deviceTb4b4b4: Te6edf3BluetoothDeviceTb4b4b4)Tb4b4b4: Te6edf3FlowTff7b72<Tffa657IntTff7b72> Tff7b72= Te6edf3deviceTb4b4b4.Te6edf3createBondTb4b4b4(Te6edf3applicationTb4b4b4)
Tff7b72internal Tff7b72suspend Tff7b72fun Td2a8ffupdateBluetoothStateTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3hasPerms Tff7b72= Te6edf3applicationTb4b4b4.Te6edf3hasBluetoothPermissionTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3newStateTb4b4b4: Te6edf3BluetoothState Tff7b72=
Te6edf3bluetoothAdapterLazyTb4b4b4.Te6edf3getTb4b4b4(Tb4b4b4)Tff7b72?.Te6edf3let Tb4b4b4{ Te6edf3adapter Tff7b72-Tff7b72>
Tff7b72val Te6edf3enabled Tff7b72= Te6edf3adapterTb4b4b4.Te6edf3isEnabled
Tff7b72val Te6edf3bondedDevices Tff7b72= Te6edf3adapterTb4b4b4.Te6edf3takeIf Tb4b4b4{ Te6edf3hasPerms Tb4b4b4}Tff7b72?.Te6edf3bondedDevices Tff7b72?: Te6edf3emptySetTb4b4b4(Tb4b4b4)
Te6edf3BluetoothStateTb4b4b4(
Te6edf3hasPermissions Tff7b72= Te6edf3hasPermsTb4b4b4,
Te6edf3enabled Tff7b72= Te6edf3enabledTb4b4b4,
Te6edf3bondedDevices Tff7b72=
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3enabledTb4b4b4) Tb4b4b4{
Te6edf3emptyListTb4b4b4(Tb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3bondedDevicesTb4b4b4.Te6edf3filter Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3nameTff7b72?.Te6edf3matchesTb4b4b4(Te6edf3RegexTb4b4b4(Te6edf3BLE_NAME_PATTERNTb4b4b4)Tb4b4b4) Tff7b72=Tff7b72= Tff7b72true Tb4b4b4}
Tb4b4b4}Tb4b4b4,
Tb4b4b4)
Tb4b4b4} Tff7b72?: Te6edf3BluetoothStateTb4b4b4(Tb4b4b4)
Te6edf3_stateTb4b4b4.Te6edf3emitTb4b4b4(Te6edf3newStateTb4b4b4)
Te6edf3debugTb4b4b4(Ta5d6ff"Ta5d6ffDetected our bluetooth access=Tffd700$Te6edf3newStateTa5d6ff"Tb4b4b4)
Tb4b4b4}
Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72const Tff7b72val Te6edf3BLE_NAME_PATTERN Tff7b72= Ta5d6ff"Ta5d6ff^.*_([0-9a-fA-F]{4})$Ta5d6ff"
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.07s